package Iterator;

import Collection01.Cat;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Grup:Iterator
 * @Auther:cola
 * @Data:2021/3/27-2021-03-21:45
 * @version:1.0
 */
public class IteratorDemo {
    //断言接口
    private static void predicateTest(){
        List<String> list=Arrays.asList("afaf","faf","法人青蛙发","擦啊地方");
        List<String> resulter= filter(list,(s)->s.contains("f"));
        resulter.forEach(System.out::println);
    }
    private static List<String> filter(List<String> list, Predicate<String> p){
        List<String> results=new ArrayList<>();
        for(String s:list){
            if(p.test(s)){//测试是否符合要求
                results.add(s);
            }
        }
        return results;
    }

    //代表结果供应商
    private static void supplierTest(){
        List<Integer> list=getNums(10,()->(int)(Math.random()*100));
        //System.out.println(list.toString());
        list.forEach(System.out::println);
    }
    private static List<Integer> getNums(int num, Supplier<Integer> sup){
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<num;i++){
            list.add(sup.get());
        }
        return list;
    }

    //表示接受一个参数并产生一个结果的函数
    private static void functionTest(){
        String s=strtoUpp("afafaw",(s1)->s1.toUpperCase());
        System.out.println(s);
    }
    private static String strtoUpp(String str,Function<String,String> f){
        return f.apply(str);
    }
    /*
    * Jdk1.8新的迭代方法
    * */
    private static void foreach(){
        List<String> list=new ArrayList<>();
        list.add("dada");
        list.add("jack");
        list.add("爱人发发");
        list.forEach(s->System.out.println(s));
        //list.forEach(System.out::println);
    }

    private static void enumration(){
        Vector<String> vs=new Vector<>();
        vs.add("dada");
        vs.add("jack");
        vs.add("爱人发发");
        Enumeration<String> es=vs.elements();
        while (es.hasMoreElements()){
            System.out.println(es.nextElement());
        }
    }
    //foreach(1.5后)
    private static void iterator1(Collection<Cat> c){
        for (Cat cat:c){
            System.out.println(cat);
        }
    }
    //iterator(1.5之前统一的迭代集合方式)
    private static void iterator2(Collection<Cat> c){
        Iterator<Cat> ite=c.iterator();
        while (ite.hasNext()){
            System.out.println(ite.next());
        }
    }

    public static void main(String[] args) {
        /*List<Cat> list=new ArrayList<>();
        Cat c1=new Cat("miaomiao",5,1);
        Cat c2=new Cat("huahua",5,2);
        Cat c3=new Cat("xiaoxiao",5,1);
        Cat c4=new Cat("miaomiao",5,3);
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        iterator1(list);
        System.out.println("-----------------------------------");
        iterator2(list);
        System.out.println("-----------------------");
        enumration();*/
        //foreach();
        //functionTest();
        //supplierTest();.

        predicateTest();
    }
}
